/*
 * Created by JFormDesigner on Fri Jun 18 13:21:08 GMT+08:00 2021
 */

package com.bk.view;

import com.bk.util.ToolUtil;

import java.awt.*;
import java.awt.event.*;
import java.sql.SQLException;
import javax.swing.*;
import javax.swing.GroupLayout;

/**
 * @author Ayi
 */
// 注册窗口类
public class RegFrame extends JFrame {

    public RegFrame() {
        initComponents();
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    // 返回登录界面，监听
    private void backLoginButtonActionPerformed(ActionEvent e) {
        // TODO add your code here
        dispose();
        new LoginFrame().setVisible(true);
    }

    // 注册，监听
    private void regButtonActionPerformed(ActionEvent e) {
        if (uname.getText().isEmpty()) {
            JOptionPane.showMessageDialog(null, "请输入注册用户名");
            return;
        }
        if (new ToolUtil().judgeIdOrName(uname.getText().trim())) {
            JOptionPane.showMessageDialog(null, "用户名不可以是纯数字！");
            return;
        }
        if (!upassword0.getText().equals(upassword1.getText())){
            JOptionPane.showMessageDialog(null, "两次密码不一致！");
            return;
        }
        if (upassword0.getText().isEmpty()){
            JOptionPane.showMessageDialog(null, "请输入注册密码");
            return;
        }
        // TODO add your code here
        ViewAction va = new ViewAction();
        String isAdmin;
        if (radioIsAdmin.isSelected()) {
            isAdmin = "是";
        }else{
            isAdmin = "否";
        }
        if (va.regAction(uname.getText(), upassword0.getText(), isAdmin)) {
            JOptionPane.showMessageDialog(null, "注册成功！");
        }else{
            JOptionPane.showMessageDialog(null, "用户名已存在");
        }
    }

    private void unameKeyTyped(KeyEvent e) {
        // TODO add your code here
        // 文本框监听，限制输入长度
        String s = uname.getText();
        if (s.length() > 7) {
            e.consume();
        }
    }

    private void upassword0KeyTyped(KeyEvent e) {
        // TODO add your code here
        // 文本框监听，限制输入长度
        String s = upassword0.getText();
        if (s.length() > 11) {
            e.consume();
        }
    }

    private void upassword1KeyTyped(KeyEvent e) {
        // TODO add your code here
        // 文本框监听，限制输入长度
        String s = upassword1.getText();
        if (s.length() > 11) {
            e.consume();
        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        // Generated using JFormDesigner Evaluation license - unknown
        label1 = new JLabel();
        uname = new JTextField();
        label2 = new JLabel();
        label3 = new JLabel();
        label4 = new JLabel();
        regButton = new JButton();
        backLoginButton = new JButton();
        upassword0 = new JPasswordField();
        upassword1 = new JPasswordField();
        radioIsAdmin = new JRadioButton();
        label5 = new JLabel();

        //======== this ========
        setTitle("\u6ce8\u518c");
        setResizable(false);
        setBackground(Color.white);
        Container contentPane = getContentPane();

        //---- label1 ----
        label1.setText("\u7528\u6237\u6ce8\u518c");
        label1.setFont(new Font("\u5fae\u8f6f\u96c5\u9ed1", Font.PLAIN, 18));

        //---- uname ----
        uname.addKeyListener(new KeyAdapter() {
            @Override
            public void keyTyped(KeyEvent e) {
                unameKeyTyped(e);
            }
        });

        //---- label2 ----
        label2.setText("\u7528\u6237\u540d ");
        label2.setFont(new Font("\u5fae\u8f6f\u96c5\u9ed1", Font.PLAIN, 14));

        //---- label3 ----
        label3.setText("\u5bc6\u7801  ");
        label3.setFont(new Font("\u5fae\u8f6f\u96c5\u9ed1", Font.PLAIN, 14));

        //---- label4 ----
        label4.setText("\u786e\u8ba4\u5bc6\u7801");
        label4.setFont(new Font("\u5fae\u8f6f\u96c5\u9ed1", Font.PLAIN, 14));

        //---- regButton ----
        regButton.setText("\u6ce8\u518c");
        regButton.setBackground(Color.white);
        regButton.setFont(new Font("\u5fae\u8f6f\u96c5\u9ed1", Font.PLAIN, 14));
        regButton.addActionListener(e -> regButtonActionPerformed(e));

        //---- backLoginButton ----
        backLoginButton.setText("\u8fd4\u56de");
        backLoginButton.setBackground(Color.white);
        backLoginButton.setFont(new Font("\u5fae\u8f6f\u96c5\u9ed1", Font.PLAIN, 14));
        backLoginButton.addActionListener(e -> backLoginButtonActionPerformed(e));

        //---- upassword0 ----
        upassword0.addKeyListener(new KeyAdapter() {
            @Override
            public void keyTyped(KeyEvent e) {
                upassword0KeyTyped(e);
            }
        });

        //---- upassword1 ----
        upassword1.addKeyListener(new KeyAdapter() {
            @Override
            public void keyTyped(KeyEvent e) {
                upassword1KeyTyped(e);
            }
        });

        //---- radioIsAdmin ----
        radioIsAdmin.setText("\u662f\u5426\u4e3a\u7ba1\u7406\u5458");
        radioIsAdmin.setFont(new Font("\u5fae\u8f6f\u96c5\u9ed1", Font.PLAIN, 12));

        //---- label5 ----
        label5.setIcon(new ImageIcon(getClass().getResource("/images/1.png")));

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
                            .addGap(0, 0, Short.MAX_VALUE)
                            .addComponent(label1, GroupLayout.PREFERRED_SIZE, 116, GroupLayout.PREFERRED_SIZE)
                            .addGap(30, 30, 30))
                        .addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
                            .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(contentPaneLayout.createParallelGroup()
                                .addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
                                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                                        .addComponent(label2)
                                        .addComponent(label3))
                                    .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                                        .addComponent(upassword0, GroupLayout.PREFERRED_SIZE, 125, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(uname, GroupLayout.PREFERRED_SIZE, 125, GroupLayout.PREFERRED_SIZE))
                                    .addGap(18, 18, 18))
                                .addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
                                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                                        .addGroup(contentPaneLayout.createSequentialGroup()
                                            .addComponent(label4)
                                            .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                                            .addComponent(upassword1, GroupLayout.PREFERRED_SIZE, 125, GroupLayout.PREFERRED_SIZE))
                                        .addGroup(contentPaneLayout.createSequentialGroup()
                                            .addComponent(regButton, GroupLayout.PREFERRED_SIZE, 95, GroupLayout.PREFERRED_SIZE)
                                            .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                                            .addComponent(backLoginButton, GroupLayout.PREFERRED_SIZE, 95, GroupLayout.PREFERRED_SIZE)))
                                    .addGap(18, 18, 18))))
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addGap(65, 65, 65)
                            .addComponent(radioIsAdmin)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addComponent(label5)
                    .addGap(9, 9, 9))
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(15, 15, 15)
                    .addComponent(label1)
                    .addGap(26, 26, 26)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(uname, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(label2))
                    .addGap(18, 18, 18)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(upassword0, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(label3))
                    .addGap(19, 19, 19)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(upassword1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(label4))
                    .addGap(18, 18, 18)
                    .addComponent(radioIsAdmin)
                    .addGap(12, 12, 12)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(backLoginButton)
                        .addComponent(regButton))
                    .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
                    .addGap(0, 0, Short.MAX_VALUE)
                    .addComponent(label5))
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    // Generated using JFormDesigner Evaluation license - unknown
    private JLabel label1;
    private JTextField uname;
    private JLabel label2;
    private JLabel label3;
    private JLabel label4;
    private JButton regButton;
    private JButton backLoginButton;
    private JPasswordField upassword0;
    private JPasswordField upassword1;
    private JRadioButton radioIsAdmin;
    private JLabel label5;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
